package com.fcy.teamwork.business.chat

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.android.api.model.Conversation
import cn.jpush.im.android.api.model.Message
import com.fcy.teamwork.App
import com.fcy.teamwork.business.recommendstudent.RecommendStudentVM
import com.fcy.teamwork.jmservice.GroupTextMsg
import com.fcy.teamwork.jmservice.JMSendService
import com.fcy.teamwork.jmservice.SingleTextMsg
import com.fcy.teamwork.jmservice.event.ChatMsgEventReceiver
import com.fcy.teamwork.pojo.SingleConversation
import com.fcy.teamwork.repo.RetrofitClient
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch

class ChatViewModel : ViewModel() {
    var targetId: String = ""
        get() {
            if (field.isEmpty()) {
                throw IllegalStateException("ViewModel未初始化成功")
            }
            return field
        }
    private val listener: ChatMsgEventReceiver by lazy {
        ChatMsgEventReceiver(targetId)
    }
    private lateinit var conversation: Conversation
    private lateinit var targetType: String
    private val singleConversationService =
        RetrofitClient.getService<RecommendStudentVM.Recommend>()

    // 暴漏给外界观察数据
    val dataSource: SharedFlow<Message> by lazy { listener.pureTextDataSource }

    fun initConversation(targetType: String, targetId: String) {
        this.targetId = targetId
        this.targetType = targetType
        conversation = when (targetType) {
            "single" -> {
                viewModelScope.launch {
                    singleConversationService.createInvitation(
                        listOf(
                            SingleConversation(
                                null,
                                App.person.id,
                                targetId.toInt()
                            )
                        )
                    )
                }
                Conversation.createSingleConversation(targetId)
            }
            "group" -> {
                Conversation.createGroupConversation(targetId.toLong())
            }
            else -> {
                throw IllegalArgumentException("参数有误")
            }
        }
        conversation.setUnReadMessageCnt(0)
    }

    fun sendMsg(msg: String): Message {
        val testMsg = conversation.createSendTextMessage(msg)
//        viewModelScope.launch {
//            when (targetType) {
//                "single" -> {
//                    JMSendService.sendSingle(SingleTextMsg(msg, targetId))
//                }
//                "group" -> {
//                    JMSendService.sendGroup(GroupTextMsg(msg, targetId.toLong()))
//                }
//
//            }
//        }
        JMessageClient.sendMessage(testMsg)
        return testMsg
    }

    fun loadTitle(): String {
        return conversation.title
    }

    fun loadMsg(): MutableList<Message> =
        conversation.allMessage


    override fun onCleared() {
        super.onCleared()
        listener.unregister()
    }

}